const app = getApp(); //引入导航

var util = require('../../utils/common.js'); //引入公共JS


var wxParse = require('../../wxParse/wxParse.js'); //引入


import { Activity } from 'activity.model.js';
var activity = new Activity();
let leftHeight = 0,
    rightHeight = 0; //分别定义左右两边的高度

let query;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    showIcon: true,
    //显示返回键以及首页
    list: [],
    leftList: [],
    rightList: [],
    num: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      leftList: [],
      rightList: []
    });
    var type = '主题乐园';
    var city = '';
    var head = 0;
    var tail = 6;
    activity.getHomeListSpecial(type, city, head, tail, res => {
      //热门推荐
      that.data.list = res;
      that.isLeft();
      that.setData({
        'getHomeList': res
      });
      tt.setStorageSync('wxcity', city);
      tt.setStorageSync('type', type);
      tt.setStorageSync('head', head);
    });
  },
  //热门推荐类型转换
  listClick: function (e) {
    var Category = e.currentTarget.dataset.list; //类型

    var num = e.currentTarget.dataset.num;
    tt.setStorageSync('type', Category);
    tt.setStorageSync('head', 0);
    var that = this;
    that.setData({
      num: num,
      leftList: [],
      rightList: []
    });
    var city = tt.getStorageSync('wxcity');
    var type = tt.getStorageSync('type');
    var head = tt.getStorageSync('head');
    var tail = 6;
    activity.getHomeListSpecial(type, city, head, tail, res => {
      //热门推荐
      that.data.list = res;
      that.isLeft();
      that.setData({
        'getHomeList': res
      });
      tt.setStorageSync('wxcity', city);
      tt.setStorageSync('type', type);
      tt.setStorageSync('head', head);
    });
  },
  //热门推荐点击进入景区详情
  DetailsClick: function (e) {
    var aid = e.currentTarget.dataset.aid;
    var jqyc = e.currentTarget.dataset.jqyc;

    if (jqyc == '景区') {
      tt.navigateTo({
        url: '../jqdetail/jqdetail?id=' + aid
      });
    } else {
      var id = e.currentTarget.dataset.id;
      tt.navigateTo({
        url: '../ycdetail/ycdetail?id=' + id
      });
    }
  },

  //上拉加載判斷高度
  async isLeft() {
    const {
      list,
      leftList,
      rightList
    } = this.data;
    query = tt.createSelectorQuery();

    for (const item of list) {
      leftHeight <= rightHeight ? leftList.push(item) : rightList.push(item); //判断两边高度，来觉得添加到那边

      await this.getBoxHeight(leftList, rightList);
    }
  },

  //輸出上拉
  getBoxHeight(leftList, rightList) {
    //获取左右两边高度
    return new Promise((resolve, reject) => {
      this.setData({
        leftList,
        rightList
      }, () => {
        query.select('#left').boundingClientRect();
        query.select('#right').boundingClientRect();
        query.exec(res => {
          leftHeight = res[0].height; //获取左边列表的高度

          rightHeight = res[1].height; //获取右边列表的高度

          resolve();
        });
      });
    });
  },

  //上拉加载
  onReachBottom(e) {
    var city = tt.getStorageSync('wxcity');
    var type = tt.getStorageSync('type');
    var head = tt.getStorageSync('head') + 6;
    var tail = 6;
    activity.getHomeListSpecial(type, city, head, tail, res => {
      //热门推荐
      console.log(res);

      if (res.code == 404) {
        console.log(111);
        tt.showToast({
          title: res.msg,
          icon: 'none'
        });
      } else {
        this.data.list = res;
        this.isLeft();
        this.setData({
          'getHomeList': res
        });
      }

      tt.setStorageSync('wxcity', city);
      tt.setStorageSync('type', type);
      tt.setStorageSync('head', head);
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {}
});